package cn.edu.zut.service;

import cn.edu.zut.dao.EmployeeDao;
import cn.edu.zut.dao.LoginLogDao;
import cn.edu.zut.domain.Employee;
import cn.edu.zut.domain.LoginLog;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class EmployeeService {
    private EmployeeDao employeeDao;
    private LoginLogDao loginLogDao;

    public boolean hasMatchEmployee(String employeeName, String password) {
        int matchCount = employeeDao.getMatchCount(employeeName, password);
        return matchCount > 0;
    }

    public Employee findEmployeeByEmployeeName(String employeeName){
        return employeeDao.findEmployeeByEmployeeName(employeeName);
    }

    @Transactional
    public void loginSuccess(Employee employee){
        employee.setCredits( 5 + employee.getCredits());//??????????????登录送5积分
        LoginLog loginLog = new LoginLog();
        loginLog.setEmployeeId(employee.getEmployeeId());
        loginLog.setIp(employee.getLastIp());
        loginLog.setLoginDate(employee.getLastVisit());
        employeeDao.updateLoginInfo(employee);
        loginLogDao.insertLoginLog(loginLog);
    }

    @Autowired
    public void setEmployeeDao(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }

    @Autowired
    public void setLoginLogDao(LoginLogDao loginLogDao) {
        this.loginLogDao = loginLogDao;
    }

}
